﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core.Business;
using System.Data;

namespace TicketManagement.Ticket
{
    public partial class Question : System.Web.UI.Page
    {
        TicketBus bus = new TicketBus();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["User"] != null)
            {
                this.displayForm();
            }
            else
            {
                Response.Redirect("/User/Login.aspx", true);
            }
            
            if (!IsPostBack)
            {
                this.showData();
            }
        }

        private void displayForm()
        {
            UserBus userBus = new UserBus();
            Core.Entity.User curUser = userBus.get(Convert.ToInt32(Session["User"]));
            if (curUser.UserTypeId == 4)
            {
                // Display customer menu
                uMenu1.Visible = true;
                uStaffMenu1.Visible = false;

                // Display customer sidebar
                uSidebar1.Visible = true;
                uStaffSidebar1.Visible = false;
            }
            else
            {
                // Display staff menu
                uMenu1.Visible = false;
                uStaffMenu1.Visible = true;
                uFooter1.Visible = false;

                // Display staff sidebar
                uSidebar1.Visible = false;
                uStaffSidebar1.Visible = true;
            }
        }

        private void showData()
        {
            String keyword = txtKeyword.Value.Trim().ToLower();

            String statuses = "";
            if (cbxNewRequset.Checked)
            {
                statuses += cbxNewRequset.Value + ",";
            }
            if (cbxProcessing.Checked)
            {
                statuses += cbxProcessing.Value + ",";
            }
            if (cbxAnswered.Checked)
            {
                statuses += cbxAnswered.Value + ",";
            }
            if (cbxProcessed.Checked)
            {
                statuses += cbxProcessed.Value + ",";
            }
            if (statuses != "")
            {
                statuses = statuses.Substring(0, statuses.LastIndexOf(','));
            }

            String searchIn = "TICKETS";
            if (rbtComment.Checked)
            {
                searchIn = "COMMENTS";
            }

            bool in2Months = rbtIn2Months.Checked;

            string orderBy = "order by TKT_NUMBER";
            if (ViewState["sortExpr"] != null)
            {
                orderBy = "order by " + ViewState["sortExpr"] + " " + GetSortDirection();
            }

            DataTable dt = bus.filter(keyword, statuses, searchIn, in2Months, orderBy);
            grvTicket.DataSource = dt;
            grvTicket.DataBind();
        }

        private string GridViewSortDirection
        {
            get { return ViewState["SortDirection"] as string ?? "DESC"; }
            set { ViewState["SortDirection"] = value; }
        }

        private string GetSortDirection()
        {
            switch (GridViewSortDirection)
            {
                case "ASC": GridViewSortDirection = "DESC"; break;
                case "DESC": GridViewSortDirection = "ASC"; break;
            }
            return GridViewSortDirection;
        }

        protected void grvTicket_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvTicket.PageIndex = e.NewPageIndex;
            this.showData();
        }

        protected void grvTicket_Sorting(object sender, GridViewSortEventArgs e)
        {
            ViewState["sortExpr"] = e.SortExpression;
            this.showData();
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.showData();
        }
    }
}